package huoye.shops.base;

import android.util.Log;

import com.codeideology.android.http.okhttp.callback.Callback;
import com.codeideology.android.lang.entity.Paging;
import com.codeideology.android.log.LogUtils;
import com.codeideology.android.rpc.RequestCallBack;
import com.codeideology.android.utils.StringUtils;
import com.google.gson.GsonBuilder;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by who on 2018/8/21.
 */

public class MineBaseJsonRequestCallback <T> extends Callback<String> implements RequestCallBack<T> {
    private Class<T> genericClass;
    private boolean isRecord;
    MinePojoJsonResponseHandler handler;

    public MineBaseJsonRequestCallback(Class<T> clz) {
        this.genericClass = clz;
        this.handler = new MinePojoJsonResponseHandler(this.genericClass, this.isRecord);
        this.getGsonBuilder();
    }

    public MineBaseJsonRequestCallback(Class<T> clz, boolean isRecord) {
        this.genericClass = clz;
        this.isRecord = isRecord;
        this.handler = new MinePojoJsonResponseHandler(this.genericClass, isRecord);
        this.getGsonBuilder();
    }

    public GsonBuilder getGsonBuilder() {
        return this.handler.getGsonBuilder();
    }

    public void onAfter(int id) {
        super.onAfter(id);
        this.onFinish();
    }

    public void onStart() {
    }

    public void onFailure(String msg, String code, Throwable e) {
        this.onFinish();
    }

    public void onFinish() {
    }

    public void onSuccess() {
    }

    public void onSuccess(T t) {
    }

    public void onSuccess(List<T> list) {
    }

    public void onSuccess(List<T> list, Paging paging) {
    }

    public void parse(String response) {
    }

    public String parseNetworkResponse(Response response, int id) throws Exception {
        return response.body().string();
    }

    public void onError(Call call, Exception e, int id) {
        LogUtils.e("test", new Object[]{e.getMessage()});
        LogUtils.e("test", new Object[]{e.getClass().getName()});
        if(!(e instanceof UnknownHostException) && !(e instanceof SocketTimeoutException) && !(e instanceof ConnectException)) {
            this.onFailure(e.getMessage(), e.getMessage(), (Throwable)null);
        } else {
            this.onNetworkFail();
            this.onFinish();
        }

    }

    public void onNetworkFail() {
    }

    public void onResponse(String response, int id) {
        try {
            LogUtils.json("test", response);
            this.handler.parse(response);
            Log.e("test","success"+this.handler.isSuccess());
            if(this.handler.isSuccess()) {
                this.onSuccess();

                List<T> list = this.handler.getParsedPojos();
                T t = (T) this.handler.getParsedPojo();
                Paging p = this.handler.getPaging();

                this.onSuccess((T) t);
                this.onSuccess(list);
                this.onSuccess(list, p);
           } else {
                this.onFailure(this.handler.getDescription(), this.handler.getStatus() + "", (Throwable)null);
            }

            this.onFinish();
        } catch (Exception var6) {
            if(StringUtils.isNotEmpty(var6.getMessage())) {
                LogUtils.e("test", new Object[]{"出错了" + var6.getMessage()});
            } else {
                LogUtils.e("test", new Object[]{"Exception"});
            }

            var6.printStackTrace();
            this.onFailure(this.handler.getDescription(), this.handler.getException(), (Throwable)null);
        }

    }
}
